var globalSaveObj = {};
$(function () {
    $.ajax({
            url:"./../server/isLogin.php",
        dataType:"json"
    }).then(function (res) {
        if (res.status==false){
            layer.confirm("你还未登录", {
                    icon: 3,
                    btn: ["立即登录", "不登录"]
                }, function () {
                    window.location.href = "./denglu.html";
                },
                function () {

                })
        }
    })
    var pid=window.location.search.split("?")[1].split("=")[1];
    $.ajax({
        url:"./../server/getGoods.php",
        data:{pid},
        dataType:"json"
    }).then(function (res) {
        globalSaveObj=res;
        $(".leftImg").attr("src","./../server/"+res.imgUrlx);
        $(".centerImg").attr("src","./../server"+res.imgUrld);
        $("#bigImg").attr("src","./../server"+res.imgUrld);
        $(".center_cent_c_t .spanm").text(res.pname);
        $(".span-name").text(res.type);
        $(".center_cent_c_t1_top .c_li1 .sp2").text("¥"+res.pprice);
        $(".omg .img-eee").attr("src","./../server/"+res.imgUrlc);
        $(".omg .img-ddd").attr("src","./../server/"+res.imgUrlj);
        $(".center_cent_c_t4").html(" <button class='btn1'>立即购买</button><button class='addCart'><a href='./gouwuche.html'>加入购物车</a></button>")
    });
})
$(function () {
    $(".center_cent").on("click",".addCart",function () {
        globalSaveObj["u_id"]=JSON.parse(localStorage.getItem("user")).u_id;
        globalSaveObj["p_number"]=$("#num").val();
        console.log(globalSaveObj)
        $.ajax({
            url:"./../server/addCarts.php",
            type:"post",
            data:globalSaveObj,
            dataType:"json"
        }).then(function (res) {
            console.log(res)
        })
    })
})

$(".center_cent_c_t3").on("click", ".jian", function () {
    var num = $(this).next("input").val();
    if (num == 1) {
        return;
    }
    $(this).next("input").val(--num);
})
$(".center_cent_c_t3").on("click", ".jia", function () {
    var num = $(this).prev("input").val();
    $(this).prev("input").val(++num);
})
$("#num").on("input", function () {
    //只能输入数字
    let val = $(this).val();
    //如果判断是否是一个数字?
    if (isNaN(val)) {
        $(this).val(1);
    }
});
$(".addCart").on("click",function () {
    location.href="./gouwuche.html";
})